<?php

header ('Content-type: text/html; charset=UTF-8'); 

//header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() - 3600));

require_once("configuration.php");

error_reporting(E_ALL OR E_NOTICE OR E_STRICT);

if(empty($_REQUEST['id']))
	$view_id	=	"";
else
	$view_id	=	$_REQUEST['id'];
	
	
        // create curl resource 
        $ch = curl_init(); 


		$parameters=array("component"=>"etl", 
								"controller"=>"view",
								"task"=>"download",
								"notation"=>$_REQUEST['notation'],
								"id"=>$view_id);
		
		$parameters= json_encode($parameters);
		
		//echo $parameters;exit();
        // set url 
        curl_setopt($ch, CURLOPT_URL, 
        				API_HOST."?parameters=".urlencode($parameters) ); 

        //return the transfer as a string 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

        // $output contains the output string 
        $output = curl_exec($ch); 

        // close curl resource to free up system resources 
        curl_close($ch); 
	
		$file_name=md5($view_id).".".$_REQUEST['notation'];
		
		header('Pragma: public'); 	// required
		header('Expires: 0');		// no cache
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
		header('Cache-Control: private',false);
		header('Content-Type: application/force-download');
		header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
		header('Content-Transfer-Encoding: binary');
		//header('Content-Length: '.filesize($file_name));	// provide file size
		header('Connection: close');
		
		
		//readfile($file_name);		// push it out
	
	
	echo $output;
	exit();
?>